﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using ProgNetComponentsDx.Options.Design;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;

namespace ProgNetComponentsDx.Options
{
    public class OptionProvider : Base.Components.BaseExtenderProvider<OptionSettings>
    {
        public OptionProvider()
        {

        }

        public OptionProvider(IContainer container)
            : base(container)
        {

        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                UnLockAllObjects();
            }
            base.Dispose(disposing);
        }

        protected override void ProcessObject(object target, OptionSettings settings)
        {
            if (settings != null)
            {
                if (OptionCache.Grabber != null)
                {
                    if (target is CheckedListBoxControl)
                    {
                        if (settings.UseMultiOptions)
                        {
                            List<CheckedListBoxItemOption> options = new List<CheckedListBoxItemOption>();
                            foreach (CheckedListBoxSettings s in settings.Options)
                            {
                                if (s.OptionId.IsNotNull())
                                    options.Add(new CheckedListBoxItemOption(s.ItemValue, s.OptionId));
                            }
                            OptionCache.PrepareCheckedListBoxControl(target as CheckedListBoxControl, options.ToArray(), settings.AutoSave);
                        }
                        else
                        {
                            if (settings.IsCustomOption)
                                OptionCache.PrepareCheckedListBoxControl(target as CheckedListBoxControl, settings.Separator, settings.CustomOptionId, settings.AutoSave, settings.CustomOptionDefaultValue);
                            else
                                OptionCache.PrepareCheckedListBoxControl(target as CheckedListBoxControl, settings.Separator, settings.OptionId, settings.AutoSave);
                        }
                    }
                    else
                    {
                        if (settings.IsCustomOption)
                            OptionCache.PrepareObject(target, settings.CustomOptionId, settings.AutoSave, settings.CustomOptionDefaultValue);
                        else
                            OptionCache.PrepareObject(target, settings.OptionId, settings.AutoSave);
                    }
                }
            }
        }

        protected override OptionSettings CreateSettings(object target)
        {
            return new OptionSettings(target, this);
        }

        public override bool CanExtend(object extendee)
        {
            if (extendee is BaseEdit ||
                extendee is BarCheckItem ||
                extendee is CheckedListBoxControl)
                return true;
            return false;
        }

        public void CommitAllOptions()
        {
            foreach (KeyValuePair<object, OptionSettings> kvp in AllObjects)
            {
                if (kvp.Value.OptionsSet && !kvp.Value.AutoSave)
                {
                    OptionCache.SetOptionFromControl(kvp.Key, false);
                }
            }
        }

        public void RefreshAllOptions()
        {
            foreach (KeyValuePair<object, OptionSettings> kvp in AllObjects)
            {
                if (kvp.Value.OptionsSet && !kvp.Value.AutoSave)
                {
                    OptionCache.SetControlValueFromOption(kvp.Key, true);
                }
            }
        }

        public void UnLockAllObjects()
        {
            foreach (KeyValuePair<object, OptionSettings> kvp in AllObjects)
            {
                if (kvp.Value.OptionsSet && !kvp.Value.AutoSave)
                {
                    OptionCache.UnLockObject(kvp.Key);
                }
            }
        }

        public void LockAllObjects()
        {
            foreach (KeyValuePair<object, OptionSettings> kvp in AllObjects)
            {
                if (kvp.Value.OptionsSet && !kvp.Value.AutoSave)
                {
                    OptionCache.LockObject(kvp.Key);
                }
            }
        }

        public object GetOptionValue(object option)
        {
            return OptionCache.GetOptionValue(option);
        }

        public bool GetOptionBoolValue(object option)
        {
            return OptionCache.GetOptionBoolValue(option);
        }

        public string GetOptionStringValue(object option)
        {
            return OptionCache.GetOptionStringValue(option);
        }

        public string GetOptionPath(object option)
        {
            foreach (KeyValuePair<object, OptionSettings> kvp in this.AllObjects)
            {
                if ((kvp.Value.OptionId != null && OptionCache.IsTheSameId(kvp.Value.OptionId, option))||
                    (kvp.Value.CustomOptionId.IsNotNull() && OptionCache.IsTheSameId(kvp.Value.CustomOptionId, option)))
                {
                    if (kvp.Key is Control)
                    {
                        Control c = kvp.Key as Control;
                        if (c != null)
                        {
                            return c.GetLocation();
                        }
                    }
                }
            }
            return "";
        }
    }
}
